<template>
	<view>
		<view class="cont" v-for="(k,m) in typeList" :key="m">
			<view class="navs" v-if="k.type == 'checkbox'">
				<view class="title">{{k.title}}</view>
				<label class="uniFlex box" v-for="(item, index) in k.items" :key="index" @click="checkboxChange(m,index)">
					<view>
						<checkbox :checked="item.checked" />
					</view>
					<view>{{item.name}}</view>
				</label>
			</view>
			<view class="navs" v-if="k.type == 'radio'">
				<view class="title">{{k.title}}</view>
				<label class="uniFlex box" v-for="(item, index) in k.items" :key="index" @click="radioChange(m,index)">
					<view>
						<radio :checked="item.checked" />
					</view>
					<view>{{item.name}}</view>
				</label>
			</view>
			<view class="navs" v-if="k.type == 'text'">
				<view class="title">{{k.title}}</view>
				<view class="uniFlex2">
					<view class="title">问题描述</view>
					<textarea type="text" v-model="k.items" placeholder="请输入" placeholder-class="pcs2"></textarea>
				</view>
			</view>
		</view>
		<view class="btnStyle" @click="submit">立即提交</view>
		<view style="height: 50upx;"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				typeList:[],
			}
		},
		onLoad(options) {
			// console.log(uni.getStorageSync('typeList'))
			uni.showLoading()
			if(uni.getStorageSync('typeList')){
				this.typeList = uni.getStorageSync('typeList');
				uni.hideLoading()
			}else{
				this.typeList = JSON.parse(JSON.parse(options.query).items)
				this.typeList.forEach(item=>{
					if(item.type == 'checkbox' || item.type == 'radio'){
						let items = []
						for(let i=0;i<item.items.length;i++){
							let str = {
								checked:false,
								name:item.items[i],
								value:item.id
							}
							items.push(str)
						}
						item.items = items
					}else{
						item.items = ''
					}
				})
				uni.hideLoading()
			}
		},
		methods: {
			checkboxChange(index1,index2){
				let typeList = this.typeList
				this.typeList[index1].items[index2].checked = !this.typeList[index1].items[index2].checked
			},
			radioChange(index1,index2) {
				this.typeList[index1].items.forEach(item=>{
					item.checked = false
				})
				this.typeList[index1].items[index2].checked = true
			},
			submit(){
				uni.showLoading()
				let typeList = this.typeList
				let chooseList = []
				typeList.forEach(item=>{
					if(item.type == 'checkbox'){
						let items = []
						for(let i=0;i<item.items.length;i++){
							if(item.items[i].checked == true){
								let str = item.id +'|'+i + ':on'
								chooseList.push(str)
							}
						}
					} else if(item.type == 'radio'){
						for(let i=0;i<item.items.length;i++){
							if(item.items[i].checked == true){
								let str = item.id + ':' + i
								chooseList.push(str)
							}
						}
					}else{
						if(item.items){
							let str = item.id + ':' + item.items
							chooseList.push(str)
						}
					}
				})
				setTimeout(function(){
					let pages = getCurrentPages(); // 当前页面
					let beforePage = pages[pages.length - 2]; // 前一个页面
					beforePage.$vm.typeDatas = chooseList//替换上个页面参数
					uni.setStorageSync('typeList',typeList)
					uni.hideLoading()
					uni.navigateBack()
				},800)
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background: #f8f8f8;
	}
	.cont{
		.navs{
			background: #ffffff;
			padding: 30upx;
			margin-bottom: 30upx;
			.title{
				margin-bottom: 20upx;
				color: #000;
				font-weight: bold;
			}
			.box{
				margin: 20upx 0;
			}
			.uniFlex2 {
				.title {
					line-height: 40px;
					width: 140upx;
					font-size: 30upx;
					font-family: Source Han Sans CN;
					font-weight: bold;
					color: #414141;
					margin-right: 40upx;
				}
			
				textarea {
					flex: 1;
					padding: 20upx 0;
				}
			}
		}
	}
	.btnStyle{
		margin-top: 50upx;
	}
</style>
